package org.electric.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.electric.bean.CompanyBean;
import org.electric.util.JDBCUtil;

/**
 * 企业dao
 * 
 * @author Administrator
 *
 */
public class Company_Dao {
	Connection coon = null;
	PreparedStatement stmt = null;
	ResultSet result = null;

	// 查询所有状态为1的企业
	public List<CompanyBean> selectCompanyByCompany_user() {

		List<CompanyBean> list = new ArrayList<CompanyBean>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from company_info where status=1 and register_status=1");
		coon = JDBCUtil.getConection();

		try {
			stmt = coon.prepareStatement(sql.toString());
			result = stmt.executeQuery();
			while (result.next()) {
				CompanyBean company = new CompanyBean();
				company.setCompany_user(result.getString("company_user"));
				company.setCompany_password(result.getString("company_password"));
				company.setCompany_name(result.getString("company_name"));
				company.setRegister_time(result.getString("register_time"));
				company.setRegister_money(result.getDouble("register_money"));
				company.setEmployee_num(result.getInt("employee_num"));
				company.setAddress(result.getString("address"));
				company.setLicense_id(result.getString("license_id"));
				company.setRegister_status(result.getInt("register_status"));
				company.setStatus(result.getInt("status"));
				company.setTel(result.getString("tel"));
				list.add(company); 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		JDBCUtil.close(coon,stmt,result);
		return list;

	}

	// 查询当前企业信息
	public List<CompanyBean> selectCompanyByCompanyuser(CompanyBean company) {

		List<CompanyBean> list = new ArrayList<CompanyBean>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from company_info where company_user=" +company.getCompany_user()+" and company_password="+company.getCompany_password()+" and status=1");
		coon = JDBCUtil.getConection();
		try {
			stmt = coon.prepareStatement(sql.toString());
			result = stmt.executeQuery();
			while (result.next()) {
				
				company.setCompany_user(result.getString("company_user"));
				company.setCompany_password(result.getString("company_password"));
				company.setCompany_name(result.getString("company_name"));
				company.setRegister_time(result.getString("register_time"));
				company.setRegister_money(result.getDouble("register_money"));
				company.setEmployee_num(result.getInt("employee_num"));
				company.setAddress(result.getString("address"));
				company.setLicense_id(result.getString("license_id"));
				company.setRegister_status(result.getInt("register_status"));
				company.setStatus(result.getInt("status"));
				company.setTel("tel");
				list.add(company);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.close(coon,stmt,result);
		return list;

	}

	// 修改
	public int updateCompany(CompanyBean company) {
		int con = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("update company_info set company_password=?,company_name=?,register_money=?,employee_num=?,address=?,tel=?"
				+ "where license_id='" + company.getLicense_id() + "'");
		coon = JDBCUtil.getConection();
		try {
			stmt = coon.prepareStatement(sql.toString());
			stmt.setString(1, company.getCompany_password());
			stmt.setString(2, company.getCompany_name());
			stmt.setDouble(3, company.getRegister_money());
			stmt.setInt(4, company.getEmployee_num());
			stmt.setString(5, company.getAddress());
			stmt.setString(6, company.getTel());
			con = stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.close(coon,stmt);
		return con;
	}

	// 删除
	public int deleteCompany(String license_id) {
		int con = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("update company_info set status=0 where license_id='" + license_id + "'");
		coon = JDBCUtil.getConection();
		try {
			stmt = coon.prepareStatement(sql.toString());

			con = stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.close(coon,stmt);
		return con;
	}

}
